#ifndef QUESTION_H
#define	QUESTION_H

using namespace std;

class Question
{
private:
    string qns;
public:
    Question();
    void insertQuestion(string, int, string);
    void editQuestion(string, int, string);
    void deleteQuestion(string, int);
    string retrieveQuestion(string, int);
};

Question::Question()
{
    qns = "";
}

void Question::insertQuestion(string topicCode, int qnNum, string question)
{
    string zSQLstr, zSQLstr2, existTable;
    stringstream ss, ss2;
    
    Database *db;
    db = new Database("test.db");
    
    ss << "select name from sqlite_master " 
       << "where type in ('table') and name = '" << topicCode << "';";
           
    zSQLstr = ss.str();

    char* zSQL = (char*)zSQLstr.c_str();

    vector<vector<string> > result = db->query(zSQL);

    for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it){
        vector<string> row = *it;
        existTable = row.at(0);
    }
    
    if(result.empty()){
        cout << "No such topic in the system" << endl;
    }else{
        ss2 << "insert into " << topicCode << " values("
        << qnNum << ", '" << question << "');";

        zSQLstr2 = ss2.str();

        char* zSQL = (char*)zSQLstr2.c_str();

        db->query(zSQL);
        
        if(db->getFail() == 1){
            cout << "Question number " << qnNum << " is already created in " << topicCode << endl;
        }else{
            cout << "Question " << qnNum << " is successfully added in " << topicCode << endl;
        }
    }
    
    db->close();
}

string Question::retrieveQuestion(string topicCode, int qnNum)
{
    string zSQLstr, zSQLstr2, existQn;
    stringstream ss, ss2;

    Database *db;
    db = new Database("test.db");

    ss << "select name from sqlite_master " 
       << "where type in ('table') and name = '" << topicCode << "';";
    
    zSQLstr = ss.str();

    char* zSQL = (char*)zSQLstr.c_str();

    vector<vector<string> > result = db->query(zSQL);

    for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it){
        vector<string> row = *it;
        existQn = row.at(0);
    }
    
    if(result.empty()){
        cout << "No such topic in the system" << endl;
    }else{
        ss2 << "select question from " << topicCode 
            << " where qnNum = " << qnNum << ";";

        zSQLstr2 = ss2.str();

        char* zSQL = (char*)zSQLstr2.c_str();

        vector<vector<string> > result = db->query(zSQL);

        for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it){
                vector<string> row = *it;
                qns = row.at(0);
        }
    }  
    db->close();
    
    return qns;
}

void Question::editQuestion(string topicCode, int qnNum, string qnNew)
{
    string zSQLstr;
    stringstream ss;

    Database *db;
    db = new Database("test.db");

    ss << "update " << topicCode << " "
        << "set question = '" << qnNew << "' "
        << "where qnNum = " << qnNum << ";";
    
    zSQLstr = ss.str();

    char* zSQL = (char*)zSQLstr.c_str();
            
    db->query(zSQL);
    
    if(db->getFail() == 1){
        cout << "Question number " << qnNum << " is already created in " << topicCode;
    }else{
        cout << "Question " << qnNum << " is successfully edited in " << topicCode;
    }
    
    db->close();
}

void Question::deleteQuestion(string topicCode, int qnNum)
{
    string zSQLstr, zSQLstr2, existTable;
    stringstream ss, ss2;
    
    Database *db;
    db = new Database("test.db");
    
    ss << "select name from sqlite_master " 
       << "where type in ('table') and name = '" << topicCode << "';";
           
    zSQLstr = ss.str();

    char* zSQL = (char*)zSQLstr.c_str();

    vector<vector<string> > result = db->query(zSQL);

    for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it){
        vector<string> row = *it;
        existTable = row.at(0);
    }
    
    if(result.empty()){
        cout << "No such topic in the system" << endl;
    }else{
        ss2 << "delete from " << topicCode << " where qnNum = " << qnNum;

        zSQLstr2 = ss2.str();

        char* zSQL = (char*)zSQLstr2.c_str();

        db->query(zSQL);
        
        if(db->getFail() == 1){
            cout << "Question number " << qnNum << " not is in " << topicCode;
        }else{
            cout << "Question " << qnNum << " is successfully deleted in " << topicCode;
        }
    }
    
    db->close();
}
#endif	/* QUESTION_H */

